﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SDK10kWrapper
{
    partial class Camera : IDisposable //http://www.codeproject.com/Articles/7792/Destructors-in-C //http://stackoverflow.com/questions/867114/why-no-reference-counting-garbage-collection-in-c //http://stackoverflow.com/questions/6967108/is-idisposable-dispose-called-automatically
    {
        /// <summary>
        /// Call this before you close the Form (on Form_Closed) to Dispose all Cameras & Reset Reference to ZERO.
        /// </summary>
        public static void DisposeAll()
        {
            // Can't use "foreach", use "for loop" because cams change Length when call instance.Dispose().
            for (int i = 0; i < cams.Count; i++)
            {
                cams[i--].Dispose(); // i-- will keep i always 0 untill Remove all the Count
            }
        }

        private bool disposed = false;

        /// <summary>
        /// Implement IDisposable Interface. Call this when you unload the Form or control. Consider to call Camera.DisposeAll()
        /// </summary>
        public void Dispose() // Explicit (the use of "using") call to this method (khac voi implicit call - Destructor (Finalizer) tu dong goi)
        {
            Dispose(true);              // Goi ham Dispose chinh
            GC.SuppressFinalize(this);  // Tranh goi Finalizer (dich noi tu Destructor <- Call Implicit form Gabarge Collector)
        }

        /// <summary>
        /// Ham dispose chinh override IDisposable (se tu dong duoc goi neu cu phap "using"
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing) // Implicit call disposing false, Explicit call disposing true
        {
            if (!disposed)
            {
                if (disposing) // Explicit Call (nghia la co thang keu ham Dispose truc tiep)
                {
                    #region Manual release of managed resources

                    #endregion
                }
            }
            #region Release unmanaged resources
            // Check if OpenInterface or not
            if (this.Handle != IntPtr.Zero)
            {
                // Stop Playing back
                KStop(this.Handle);

                // Stop Recording
                MP4FILE_RECORD_INFO mrec = new MP4FILE_RECORD_INFO();
                KStopRecord(this.Handle, ref mrec);

                // Stop Streaming
                KStopStreaming(this.Handle);

                // Disconnect Server
                KDisconnect(this.Handle);

                // Close Interface
                KCloseInterface(this.Handle);
            }
            #endregion

            // Remove from cams
            cams.Remove(this);
            count--;

            disposed = true;
        }

        ~Camera()
        {
            Dispose(false);
        }
    }
}
